package com.dayatang.android.config;

import java.util.concurrent.ExecutionException;

import android.content.Context;

import com.dayatang.android.Model.Direction;
import com.dayatang.android.Task.NavigationTask;
import com.google.gson.Gson;

/**
 * 获取导航路线
 * 
 * @author mluo
 * 
 */
public class NavigationConfig extends BaseConfig {

	private static Direction direction;

	public static Direction getNavigationDirection(Context context, String url) {
		performRequest(context, url);
		if (direction == null) {
			return null;
		}
		return direction;
	}

	/**
	 * 解析Direction JSON
	 * 
	 * @param jsonData
	 * @return
	 */
	private static Direction parseDirection(String json) {
		Gson gson = new Gson();
		return gson.fromJson(json, Direction.class);
	}

	private static void performRequest(final Context context, final String url) {
		try {
			String data = new NavigationTask(context).execute(url).get();
			direction = parseDirection(data);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}

}
